package com.example.administrator.androidros.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.administrator.androidros.R;
import com.hhc.netty.DeviceModel;

import java.util.List;

/**
 * Created by huhaichao on 2018/3/26.
 */

public class SceneDetailsAdapter extends RecyclerView.Adapter<SceneDetailsAdapter.ViewHolder> {
    private Context mContext;
    private List<DeviceModel> data;

    public SceneDetailsAdapter(Context mContext, List<DeviceModel> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_scene_item_scene_detail, parent, false);
        ViewHolder mViewHolder = new ViewHolder(view);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (data.get(position).getApplianceType().equals("10")) {
            holder.activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        } else if (data.get(position).getApplianceType().equals("20")) {
            holder.activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        } else if (data.get(position).getApplianceType().equals("30")) {
            holder.activity_scene_item_scene_detail_img_icon.setImageResource(R.mipmap.ic_launcher);
        }
        holder.activity_scene_item_scene_detail_tv_name.setText(data.get(position).getApplianceName());

        //注意 setOnClickListener 和 setClickable 的先后问题 //setClickable-->如果设置了setOnClickListener，则setClickable无效
        holder.activity_scene_item_scene_detail_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.activity_scene_item_scene_detail_switch.isChecked()) {
                    onSwipeClickListener.onClick(position, holder.activity_scene_item_scene_detail_switch.isChecked());
                } else {
                    onSwipeClickListener.onClick(position, holder.activity_scene_item_scene_detail_switch.isChecked());
                }
            }
        });


        if (data.get(position).getOnlineStatus() == null || data.get(position).getOnlineStatus().equals("")) {
            holder.activity_scene_item_scene_detail_tv_status.setText("离线");
            holder.activity_scene_item_scene_detail_tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.red));
            //同时开关按钮不能点击
            holder.activity_scene_item_scene_detail_switch.setEnabled(false);//如果设置了setOnClickListener，则无效
        } else {
            // 设置设备在线状态 -> 10:在线 20:离线
            if (data.get(position).getOnlineStatus().equals("10")) {
                holder.activity_scene_item_scene_detail_tv_status.setText("在线");
                holder.activity_scene_item_scene_detail_tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.lawn_green));
                holder.activity_scene_item_scene_detail_switch.setEnabled(true);//如果设置了setOnClickListener，则无效
            } else if (data.get(position).getOnlineStatus().equals("20")) {
                holder.activity_scene_item_scene_detail_tv_status.setText("离线");
                holder.activity_scene_item_scene_detail_tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.red));

                //同时开关按钮不能点击
                holder.activity_scene_item_scene_detail_switch.setEnabled(false);
            }
        }

        // 设置设备状态 -> 10:开 20:关
        if (null != data.get(position).getStatus() && !data.get(position).getStatus().equals("")) {
            if (data.get(position).getStatus().equals("10")) {
                holder.activity_scene_item_scene_detail_switch.setChecked(true);
            } else if (data.get(position).getStatus().equals("20")) {
                holder.activity_scene_item_scene_detail_switch.setChecked(false);
            }
        }
//        holder.activity_scene_item_scene_detail_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Bundle bundle = new Bundle();
////                bundle.putString("sceneId", data.get(position).getSceneId());
////                bundle.putString("title", data.get(position).getSceneName());
////                Intent intent = new Intent();
////                intent.setClass(mContext, DeviceActivity.class);
////                intent.putExtras(bundle);
////                mContext.startActivity(intent);
//                if (onSwipeClickListener != null) {
//                    onSwipeClickListener.onClick(position);
//                }
//            }
//        });

//        holder.activity_scene_item_scene_detail_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    onSwipeClickListener.onClick(position,isChecked);
//                } else {
//                    onSwipeClickListener.onClick(position,isChecked);
//                }
//            }
//        });

        holder.activity_scene_item_scene_detail_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.activity_scene_item_scene_detail_switch.isChecked()) {
                    onSwipeClickListener.onClick(position, holder.activity_scene_item_scene_detail_switch.isChecked());
                } else {
                    onSwipeClickListener.onClick(position, holder.activity_scene_item_scene_detail_switch.isChecked());
                }
            }
        });

        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSwipeClickListener != null) {
                    onSwipeClickListener.onDelete(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public List<DeviceModel> getDataList() {
        return data;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView activity_scene_item_scene_detail_img_icon;
        private TextView activity_scene_item_scene_detail_tv_name;
        private TextView activity_scene_item_scene_detail_tv_status;
        private Switch activity_scene_item_scene_detail_switch;
        private RelativeLayout activity_scene_item_scene_detail_item;
        private Button btnDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            activity_scene_item_scene_detail_img_icon = (ImageView) itemView.findViewById(R.id.activity_scene_item_scene_detail_img_icon);
            activity_scene_item_scene_detail_tv_name = (TextView) itemView.findViewById(R.id.activity_scene_item_scene_detail_tv_name);
            activity_scene_item_scene_detail_tv_status = (TextView) itemView.findViewById(R.id.activity_scene_item_scene_detail_tv_status);
            activity_scene_item_scene_detail_switch = (Switch) itemView.findViewById(R.id.activity_scene_item_scene_detail_switch);
            activity_scene_item_scene_detail_item = (RelativeLayout) itemView.findViewById(R.id.activity_scene_item_scene_detail_item);
            btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
        }
    }

    public void setNewData(List<DeviceModel> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public interface OnSwipeClickListener {
        void onClick(int position,boolean status);

        void onDelete(int position);
    }

    private OnSwipeClickListener onSwipeClickListener;

    public void setOnSwipeClickListener(OnSwipeClickListener onSwipeClickListener) {
        this.onSwipeClickListener = onSwipeClickListener;
    }
}
